package com.bsb.biz.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.common.config.Global;
import com.bsb.base.common.util.DateUtils;
import com.bsb.base.common.util.IdGen;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbCase;
import com.bsb.base.model.BsbCaseExample;
import com.bsb.base.model.BsbCaseExample.Criteria;
import com.bsb.biz.service.BsbCaseService;
import com.bsb.biz.shiro.UserUtils;

@Controller
@RequestMapping("/bsbCase")
public class BsbCaseController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbCase/list.do";
	
	@Resource
	private BsbCaseService bsbCaseService;
	
	@RequiresPermissions("bsbCase:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		String title = (String) params.get("title");
		String type = (String) params.get("type");
		String delFlag = (String) params.get("delFlag");
		String start = (String) params.get("start");
		Date startDate = DateUtils.parseDate(start);
		String end = (String) params.get("end");
		Date endDate = DateUtils.parseDate(end);
		BsbCaseExample bsbCaseExample = new BsbCaseExample();
		Criteria criteria = bsbCaseExample.createCriteria();
		if(StringUtils.isNotEmpty(title)){
			criteria.andTitleLike("%"+title+"%");
		}
		if(StringUtils.isNotEmpty(type)){
			criteria.andTypeEqualTo(type);
		}
		if(StringUtils.isNotEmpty(delFlag)){
			criteria.andDelFlagEqualTo(delFlag);
		}
		if(null != startDate){
			criteria.andCreateDateGreaterThanOrEqualTo(startDate);
		}
		if(null != endDate){
			endDate = DateUtils.addDays(endDate, 1);
			criteria.andCreateDateLessThanOrEqualTo(endDate);
		}
		int totalCount = bsbCaseService.countBsbCase(bsbCaseExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbCaseExample.setPageIndex((pageNum - 1) * pageSize);
		bsbCaseExample.setPageSize(pageSize);
		bsbCaseExample.setOrderByClause("createDate DESC");
		List<BsbCase> bsbCases = bsbCaseService.getPageBsbCase(bsbCaseExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbCases", bsbCases);
		model.addAttribute("title", title);
		model.addAttribute("type", type);
		model.addAttribute("delFlag", delFlag);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		return "/BsbCase/list";
	}
	
	@RequiresPermissions("bsbCase:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/BsbCase/add";
	}

	@RequiresPermissions("bsbCase:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model, @ModelAttribute("bsbCase") BsbCase bsbCase) {
		bsbCase.setUuid(IdGen.uuid());
		bsbCase.setCreateBy(UserUtils.currentUser().getId().toString());
		bsbCase.setCreateDate(new Date());
		bsbCaseService.addBsbCase(bsbCase);
		return redirect;
	}
	
	@RequiresPermissions("bsbCase:delete")
	@RequestMapping(value = "/{uuid}/delete", method = RequestMethod.GET)
	public String del(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("uuid") String uuid) {
		String queryString = request.getQueryString();
		bsbCaseService.delBsbCaseByUuid(uuid);
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbKnowledge:delete")
	@RequestMapping(value = "/{uuid}/show", method = RequestMethod.GET)
	public String show(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("uuid") String uuid) {
		String queryString = request.getQueryString();
		BsbCase bsbCase = bsbCaseService.getBsbCaseByUuid(uuid);
		if(null != bsbCase){
			bsbCase.setDelFlag(Global.SHOW);
			bsbCase.setUpdateDate(new Date());
			bsbCaseService.updateBsbCase(bsbCase);
		}
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbKnowledge:delete")
	@RequestMapping(value = "/{uuid}/hide", method = RequestMethod.GET)
	public String hide(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("uuid") String uuid) {
		String queryString = request.getQueryString();
		BsbCase bsbCase = bsbCaseService.getBsbCaseByUuid(uuid);
		if(null != bsbCase){
			bsbCase.setDelFlag(Global.HIDE);
			bsbCase.setUpdateDate(new Date());
			bsbCaseService.updateBsbCase(bsbCase);
		}
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbCase:update")
	@RequestMapping(value = "/{uuid}/update", method = RequestMethod.GET)
	public String toEdit(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("uuid") String uuid) {
		BsbCase bsbCase = bsbCaseService.getBsbCaseByUuid(uuid);
		String queryString = request.getQueryString();
		model.addAttribute("queryString", queryString);
		model.addAttribute("bsbCase", bsbCase);
		return "/BsbCase/edit";
	}

	@RequiresPermissions("bsbCase:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model,@RequestParam String queryString,
			@ModelAttribute("bsbCase") BsbCase bsbCase) {
		bsbCase.setUpdateDate(new Date());
		bsbCaseService.updateBsbCase(bsbCase);
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbCase:view")
	@RequestMapping(value = "/{uuid}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("uuid") String uuid) {
		BsbCase bsbCase = bsbCaseService.getBsbCaseByUuid(uuid);
		model.addAttribute("bsbCase", bsbCase);
		return "/BsbCase/view";
	}
}
